package com.eam.controller;

import com.eam.service.SparePartsRecordService;
import com.yy.base.page.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("SparepartsRecord")
public class SparePartsRecordController {
	@Autowired
	private SparePartsRecordService sparepartsrecordService;
	
	 
	//加载设备备品配件领用记录信息
	@ResponseBody
	@RequestMapping(value="InquireallRecord",method=RequestMethod.POST)
	public Message InquireallRecord(HttpServletRequest request){
		Message msg = new Message();
			
		msg.setData("Stocks",sparepartsrecordService.getSparepartsList());
		
		
		return msg;
	}
	
	//加载库存数量
		@ResponseBody
		@RequestMapping(value="SumMaterielsresult",method=RequestMethod.POST)
	public Message SumMaterielsresult(HttpServletRequest request){
		Message msg = new Message();
					
		String toolCode = request.getParameter("toolCode").toString();   //领用的物料编码
				
			//查询当前物料的库存数量
		msg.setData("sumnumbers",sparepartsrecordService.getSumMateriels(toolCode));
			
		
				
		return msg;
	}	

	
	//将设备管理人员对于设备备品配件的领料单审核结果进行操作
	@ResponseBody
	@RequestMapping(value="SparepartsRecordexamine",method=RequestMethod.POST)
	public Message SparepartsRecordexamine(HttpServletRequest request){
		Message msg = new Message();
		
		String KCNUMBERS = request.getParameter("KCNUMBERS"); //库存数量
		String CKNUMBERS = request.getParameter("CKNUMBERS"); //领用数量(管理人员修改后的领用数量实际领用数量)
		String YCCKNUMBERS = request.getParameter("YCCKNUMBERS"); //领用数量(未修改的领用数量)
		String MATERIEL = request.getParameter("MATERIEL");   //物料
		String WLLYJL = request.getParameter("WLLYJL");		  //记录ID
		String RESULTS = request.getParameter("RESULTS");	  //审核结果
		
		int COLLARs = Integer.parseInt(CKNUMBERS);      //转化后的领用数量（修改后的领用数量）
		int YCCOLLARs = Integer.parseInt(YCCKNUMBERS);      //转化后的领用数量(隐藏领用数量)
  		int KUSHULIANGs = Integer.parseInt(KCNUMBERS); //转化后的库存数量
  		
  		
		int sumnumbers  = COLLARs + KUSHULIANGs; 
		
		//修改数量后需要返回的数量加上现有库存数量则为审核后的库存数量
		int updateqty = 0;
		
		if(COLLARs > YCCOLLARs){
			updateqty = KUSHULIANGs-(COLLARs - YCCOLLARs);
		}else if(COLLARs < YCCOLLARs ){
			updateqty = KUSHULIANGs+(YCCOLLARs - COLLARs);
		}else{
			updateqty = KUSHULIANGs;
		}
		
		if(RESULTS.equals("passed")){
			//审核通过只改变状态
			sparepartsrecordService.getUpdaeteSparePartsRecord(RESULTS,WLLYJL,updateqty,COLLARs);
			//sparepartsrecordService.getUpdaeteSpareParts(updateqty,MATERIEL);
		}else if(RESULTS.equals("Notthrough")){
			//审核不通过领用数量返回库存状态改变未不通过
			//sparepartsrecordService.getUpdaeteSpareParts(sumnumbers,MATERIEL);
			sparepartsrecordService.getUpdaeteSparePartsRecords(RESULTS,WLLYJL);
		}
		
		return msg;
			
	}
	

  		
  		
}
	
	
	
	
